<?php

namespace App\Console\Commands;

use App\Model\Permission;
use App\Model\Role;
use App\Model\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class RoleInit extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'system:init';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '权限初始化';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        DB::table('permissions')->delete();
        DB::table('permission_role')->delete();
        DB::table('role_user')->delete();
        DB::table('roles')->delete();
        $adminuser = User::query()->where('username','admin')->first();
        if(!$adminuser){
            $adminuser = new  User();
        }
        $adminuser->type = 'admin';
        $adminuser->username = 'admin';
        $adminuser->password =  bcrypt('password');
        $adminuser->api_token = str_random(60);
        $adminuser->save();
        $admin = new Role();
        $admin->name = 'admin';
        $admin->display_name = '后台管理员';
        $admin->description = '后台管理员';
        $admin->save();
        $adminRoles  = Role::select('id')->where('name','admin')->get()->toArray();
        $adminuser->attachRoles($adminRoles);
        $mediator = new Role();
        $mediator->name = 'mediator';
        $mediator->display_name = '调解人员';
        $mediator->description = '调解人员';
        $mediator->save();
        $financial = new Role();
        $financial->name = 'financial';
        $financial->display_name = '财务人员';
        $financial->description = '财务人员';
        $financial->save();
        $judicial = new Role();
        $judicial->name = 'judicial';
        $judicial->display_name = '司法人员';
        $judicial->description = '司法人员';
        $judicial->save();

        $RoleManager= new Permission();
        $RoleManager->name = 'RoleManager';
        $RoleManager->display_name = '权限设置权限';
        $RoleManager->description = '权限设置权限';
        $RoleManager->save();

        $userManager= new Permission();
        $userManager->name = 'userManager';
        $userManager->display_name = '组织队伍管理';
        $userManager->description = '组织队伍管理权限';
        $userManager->save();

        $organizationManager= new Permission();
        $organizationManager->name = 'organizationManager';
        $organizationManager->display_name = '调解组织管理';
        $organizationManager->description = '调解组织管理权限';
        $organizationManager->save();

        $mediatorManager= new Permission();
        $mediatorManager->name = 'mediatorManager';
        $mediatorManager->display_name = '调解员管理';
        $mediatorManager->description = '调解员管理权限';
        $mediatorManager->save();

        $financialManager= new Permission();
        $financialManager->name = 'financialManager';
        $financialManager->display_name = '财务人员管理';
        $financialManager->description = '财务人员管理权限';
        $financialManager->save();

        $judicialManager= new Permission();
        $judicialManager->name = 'judicialManager';
        $judicialManager->display_name = '司法人员管理';
        $judicialManager->description = '司法人员管理权限';
        $judicialManager->save();

        $trainingApplication= new Permission();
        $trainingApplication->name = 'trainingApplication';
        $trainingApplication->display_name = '培训申请管理';
        $trainingApplication->description = '培训申请管理权限';
        $trainingApplication->save();

        $organizationCommendation= new Permission();
        $organizationCommendation->name = 'organizationCommendation';
        $organizationCommendation->display_name = '组织荣誉管理';
        $organizationCommendation->description = '组织荣誉管理权限';
        $organizationCommendation->save();

        $mediatorCommendation= new Permission();
        $mediatorCommendation->name = 'mediatorCommendation';
        $mediatorCommendation->display_name = '调解员表彰管理';
        $mediatorCommendation->description = '调解员表彰管理权限';
        $mediatorCommendation->save();

        $fundingProtection= new Permission();
        $fundingProtection->name = 'fundingProtection';
        $fundingProtection->display_name = '经费保障统计管理';
        $fundingProtection->description = '经费保障统计管理权限';
        $fundingProtection->save();

        $caseManager= new Permission();
        $caseManager->name = 'caseManager';
        $caseManager->display_name = '案件办理管理';
        $caseManager->description = '案件办理管理权限';
        $caseManager->save();

        $daily = new Permission();
        $daily->name = 'daily';
        $daily->display_name = '日常事务管理';
        $daily->description = '日常事务管理权限';
        $daily->save();

        $caseStatistics = new Permission();
        $caseStatistics->name = 'caseStatistics';
        $caseStatistics->display_name = '案件情况统计';
        $caseStatistics->description = '案件情况统计权限';
        $caseStatistics->save();

        $subsidyApplication = new Permission();
        $subsidyApplication->name = 'subsidyApplication';
        $subsidyApplication->display_name = '补贴申请';
        $subsidyApplication->description = '补贴申请权限';
        $subsidyApplication->save();

        $trainingSituation = new Permission();
        $trainingSituation->name = 'trainingSituation';
        $trainingSituation->display_name = '培训情况';
        $trainingSituation->description = '培训情况管理权限';
        $trainingSituation->save();


        $caseCreateManager= new Permission();
        $caseCreateManager->name = 'caseCreateManager';
        $caseCreateManager->display_name = '新增案件';
        $caseCreateManager->description = '新增案件权限';
        $caseCreateManager->save();

        $caseInfoManager= new Permission();
        $caseInfoManager->name = 'caseInfoManager';
        $caseInfoManager->display_name = '案件管理';
        $caseInfoManager->description = '案件管理权限';
        $caseInfoManager->save();

        $subsidyAccounting = new Permission();
        $subsidyAccounting->name = 'subsidyAccounting';
        $subsidyAccounting->display_name = '补贴核算';
        $subsidyAccounting->description = '补贴核算权限';
        $subsidyAccounting->save();

        $knowledge= new Permission();
        $knowledge->name = 'knowledge';
        $knowledge->display_name = '知识库管理';
        $knowledge->description = '知识库管理权限';
        $knowledge->save();

        $classicCase= new Permission();
        $classicCase->name = 'classicCase';
        $classicCase->display_name = '典型案例管理';
        $classicCase->description = '典型案例管理权限';
        $classicCase->save();

        $law= new Permission();
        $law->name = 'law';
        $law->display_name = '法律法规管理';
        $law->description = '法律法规管理权限';
        $law->save();

        $explain= new Permission();
        $explain->name = 'explain';
        $explain->display_name = '司法解释管理';
        $explain->description = '司法解释管理权限';
        $explain->save();


        $Permissions  = Permission::select('id')->get()->toArray();
        $admin->attachPermissions($Permissions);
    }
}
